//
//  SCFormModel.m
//  Shuaiche
//
//  Created by 党帅 on 2019/7/30.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "SCFormModel.h"

@implementation SCFormSectionModel

- (instancetype)init {
    self = [super init];
    if (self) {
        _rowHeight = 44;
    }
    return self;
}

+ (SCFormSectionModel *)sectionWithRowKeys:(NSArray *)keys {
    SCFormSectionModel *section = [[SCFormSectionModel alloc] init];
    for (NSString *key in keys) {
        SCFormModel *model = [[SCFormModel alloc] init];
        model.key = key;
        [section.rows addObject:model];
    }
    return section;
}

+ (SCFormSectionModel *)sectionWithRowValues:(NSArray *)values {
    SCFormSectionModel *section = [[SCFormSectionModel alloc] init];
    for (NSString *key in values) {
        SCFormModel *model = [[SCFormModel alloc] init];
        model.value = key;
        [section.rows addObject:model];
    }
    return section;
}

- (void)addForm:(SCFormModel *)model {
    [self.rows addObject:model];
}

- (void)addFormsFromKeys:(NSArray *)keys {
    for (NSString *key in keys) {
        if ([key isKindOfClass:SCFormModel.class]) {
            [self.rows addObject:key];
        } else {
            SCFormModel *model = [[SCFormModel alloc] init];
            model.key = key;
            [self.rows addObject:model];
        }
    }
}

- (NSInteger)rowHeightForIndex:(NSIndexPath *)indexPath {
    SCFormModel *model = self.rows[indexPath.row];
    if (model.height) {
        return model.height;
    }
    if ([model isKindOfClass:SCFormModel.class] &&
        [model.key isEqualToString:@"line"]) {
        model.height = 1;
        return 1;
    }
    return _rowHeight;
}

- (NSMutableArray *)rows {
    if (!_rows) {
        _rows = [NSMutableArray arrayWithCapacity:0];
    }
    return _rows;
}

- (NSString *)titleString {
    return self.headerTitle;
}

@end


@implementation SCFormModel


- (NSString *)titleString {
    return self.value;
}

- (void)setValue:(NSString *)value {
    _value = value;
    if (self.valueChanged) {
        self.valueChanged(value);
    }
}

@end
